package com.bzkj.web.controller.monitor;

import com.bzkj.common.annotation.Log;
import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.poi.ExcelPDFUtil;
import com.bzkj.system.domain.SysBasicDataLog;
import com.bzkj.system.service.ISysBasicDataLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 基础数据操作日志记录
 *
 * @author ruoyi
 */
@Api(tags = "系统日志")
@RestController
@RequestMapping("/monitor/basic/operlog")
public class SysBasicDataOperlogController extends BaseController {
    @Autowired
    private ISysBasicDataLogService basicDataLogService;

    /**
     * 获取单据操作日志列表
     *
     * @param operLog 查询条件
     * @return 查询结果
     */
    @ApiOperation("查询接口")
    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBasicDataLog operLog) {
        startPage();
        List<SysBasicDataLog> list = basicDataLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }

    /**
     * 单据操作日志导出
     * @param response 响应对象
     * @param operLog 查询条件
     */
    @ApiOperation("导出接口")
    @LogC(title = "基础数据操作日志", businessType = BusinessType.EXPORTPDF)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping("/export")
    public AjaxResult export(HttpServletResponse response, SysBasicDataLog operLog) {
        operLog.setIsExport(1);
        List<SysBasicDataLog> list = basicDataLogService.selectOperLogListExport(operLog);
        Map<String, Object> params = operLog.getParams();
        String logDate = null;
        if (params != null) {
            if (params.get("beginTime") != null && params.get("endTime") != null) {
                logDate = params.get("beginTime") + " 至 " + params.get("endTime");
            }
        }
        ExcelPDFUtil<SysBasicDataLog> util = new ExcelPDFUtil<>(SysBasicDataLog.class);
        util.exportPDF(response, list, "操作日志", "基础数据操作日志", getUsername(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), logDate != null ? logDate : "全量");
        return AjaxResult.success();
    }


    /**
     * 批量删除操作日志
     * @param operIds 操作日志主键数组
     * @return 操作结果
     */
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds) {
        return toAjax(basicDataLogService.deleteOperLogByIds(operIds));
    }

    /**
     * 清空操作日志
     * @return 操作结果
     */
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        basicDataLogService.cleanOperLog();
        return success();
    }

    /**
     * 根据id查询操作日志详情
     *
     * @param id 数据主键
     * @return 查询结果
     */
    @ApiOperation("详情接口")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    public AjaxResult selectDetailsById(@PathVariable("id") Long id) {
        return AjaxResult.success(basicDataLogService.selectOperLogById(id));
    }

}
